//
//  CMDBPasswordManager.swift
//  CMDB
//
//  Created by 王磊 on 17/4/5.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBPasswordManager: NSObject {
    static let getInstance: CMDBPasswordManager = CMDBPasswordManager()
    
    private override init() {
        
    }
}
extension CMDBPasswordManager: CMDBPasswordManagerDelegate {
    
    func modifyPwd(pwdParam: CMDBModifyPwdParam, handler: CMDBPasswordHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard pwdParam.password.cmdb_length >= Password_Min_Length else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.PasswordInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid)
        }
        guard pwdParam.newPassword.cmdb_length >= Password_Min_Length else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.NewPasswordInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.NewPasswordInvalid)
        }
        
        guard pwdParam.confirmPassword.cmdb_length >= Password_Min_Length else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.NewPasswordInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.ConfirmPasswordInvalid)
        }
        
        
        guard pwdParam.confirmPassword == pwdParam.newPassword else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.OldPwdNotMatchNewPwd.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.OldPwdNotMatchNewPwd)
        }
        
        CMDBHttpManager.getInstance.request(pwdParam.URLString(), param: pwdParam.keyValues, success: { (sysTime, data, page) in
            
            handler(isSuccess: true)
            
            }, error: error, failure: failure)
    }
    
    func pwdBack(pwdParam: CMDBPwdBackParam, handler: CMDBPasswordHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard pwdParam.pwd.cmdb_length >= Password_Min_Length else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.PasswordInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid)
        }
        guard pwdParam.submitPwd.cmdb_length >= Password_Min_Length else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.PasswordInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid)
        }
        
        guard pwdParam.pwd == pwdParam.submitPwd else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.OldPwdNotMatchNewPwd.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.OldPwdNotMatchNewPwd)
        }
        
        CMDBHttpManager.getInstance.request(pwdParam.URLString(), param: pwdParam.keyValues, success: { (sysTime, data, page) in
            
            handler(isSuccess: true)
            
            }, error: error, failure: failure)
    }
}
extension CMDBPasswordManager {
    
    func verifyPwd(pwdParam: CMDBVerifyPwdParam, handler: CMDBPasswordHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard pwdParam.password.cmdb_length >= Password_Min_Length else {
            
            printLog("\(CMDBUserControllerExceptionCodeType.PasswordInvalid.rawValue)")
            
            throw CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.PasswordInvalid)
        }
        
        CMDBHttpManager.getInstance.request(pwdParam.URLString(), param: pwdParam.keyValues, success: { (sysTime, data, page) in
            
            handler(isSuccess: true)
            
            }, error: error, failure: failure)
    }
    
}
